package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.TradeList;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.network.serverpackets.TradeItemChange;

/**
 *  增加一个交易物品
 *  客户端请求协议
 * @author zjj
 *
 */
public final class ChangeTradeItem extends L2GameClientPacket {
	
	private static final String _C__75_ADDTRADEITEM = "[C] 0x75 AddTradeItem";

	private static final Logger _log = LoggerFactory.getLogger(ChangeTradeItem.class.getName());

	private static final int TRADE_OP_ADD = 0;

	private static final int TRADE_OP_DEL = 1;

	private int _tradeId; //  0:增加1:删除

	private long _objectId;

	private int _count;

	public ChangeTradeItem() {
	}

	@Override
	protected void readImpl() {
		_tradeId = readByte();
		_objectId = readLong();
		_count = readInt();
	}

	@Override
	protected void runImpl() {
		final FuzePcInstance player = getClient().getActiveChar();
		if (player == null)
			return;

		final TradeList trade = player.getActiveTradeList();
		if (trade == null) {
			_log.warn("Character: " + player.getName() + " requested item:" + _objectId + " add without active tradelist:" + _tradeId, Config.SERVER_ID);
			return;
		}

		final FuzePcInstance partner = trade.getPartner();
		if (partner == null || player.getKnownList().getKnownCharacters().get(partner.getObjectId()) == null || partner.getActiveTradeList() == null) {
			// Trade partner not found, cancel trade
			if (partner != null)
				_log.warn("Character:" + player.getName() + " requested invalid trade object: " + _objectId, Config.SERVER_ID);
			SystemMessage msg = new SystemMessage(SystemMessageId.TARGET_IS_NOT_FOUND_IN_THE_GAME);
			player.sendPacket(msg);
			player.cancelActiveTrade();
			return;
		}

		if (trade.isConfirmed() || partner.getActiveTradeList().isConfirmed()) {
			player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_ADJUST_ITEMS_AFTER_TRADE_CONFIRMED));
			return;
		}

		if (!player.getAccessLevel().allowTransaction()) {
			player.sendMessage("Transactions are disable for your Access Level");
			player.cancelActiveTrade();
			return;
		}

		if (!player.validateItemManipulation(_objectId, "trade")) {
			player.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
			return;
		}

		TradeList.TradeItem item = null;
		if (_tradeId == TRADE_OP_ADD) //增加一个物品
		{
			item = trade.addItem(_objectId, _count);
		} else //删除一个物品
		{
			item = trade.removeItem(_objectId, 1, _count);
		}

		if (item != null) {
			player.sendPacket(new TradeItemChange(item, TradeItemChange.ITEM_LIST_SELF, _count, _tradeId));
			trade.getPartner().sendPacket(new TradeItemChange(item, TradeItemChange.ITEM_LIST_OTHER, _count,  _tradeId));
		}

	}// end 

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__75_ADDTRADEITEM;
	}
}
